/**  
 * @Title:  DownLoadController.java   
 * @Package com.gtjy.p2p.modules.common.controller   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: 吴元森 
 * @date:   2019年5月6日 下午6:08:55   
 * @version V1.0 
 * @Copyright: 2019 吴元森.
 */
package com.platform.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.logger.annotation.ApiLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**   
 * @ClassName:  DownLoadController   
 * @Description:下载指定文件
 * @author: 吴元森 
 * @date:   2019年5月6日 下午6:08:55   
 *     
 * @Copyright: 2019 吴元森.  
 */
@Api(tags= {"COMMON-文件下载服务"})
@Controller
@RequestMapping("common/download")
public class DownLoadController {
	
	/**
	 * @Title: downLoadFile   
	 * @Description: 下载指定单个文件
	 * @param response
	 * @param request
	 * @param downLoadFilePath	文件路径
	 * @param downLoadFileNme   文件名称
	 * void
	 */
	@ApiLog(value="下载指定单个文件",desc="下载指定单个文件")
	@RequestMapping(value="single",method=RequestMethod.GET)
	@ApiOperation("下载指定单个文件")
	@ResponseBody
	public void downLoadFile(HttpServletResponse response,HttpServletRequest request,String downLoadFilePath,String downLoadFileNme) {
		File target = new File(downLoadFilePath);
		String fileName = target.getName();
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
		OutputStream out = null;
		try {
			response.reset();
			response.setBufferSize(5 * 1024 * 1024);
			response.addHeader("Content-Length", "" + target.length());
			response.setHeader("Content-Disposition", "attachment; filename=" + new String((downLoadFileNme+"."+suffix).getBytes("gb2312"), "iso8859-1"));
			response.setContentType("application/octet-stream");
			out = response.getOutputStream();
			out.write(FileUtils.readFileToByteArray(target));
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
